<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input v-model="query.taskName" size="small" class="filter-item" clearable style="width: 200px" placeholder="请输入任务名称" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.status" class="filter-item" placeholder="请选择测试状态" filterable clearable size="small" style="width:200px">
          <el-option v-for="item in statusOptions" :key="item.key" :label="item.label" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!-- 功能按钮 -->
      <crudOperation :permission="permission" />
    </div>
    <!-- 数据列表 -->
    <el-table
      ref="table"
      v-loading="crud.loading"
      style="width: 100%;"
      :data="crud.data"
      @filter-change="crud.handleFilterChange"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column v-if="columns.visible('taskName')" :show-overflow-tooltip="true" prop="taskName" label="任务名称" min-width="150" />
      <el-table-column v-if="columns.visible('migrationType')" :show-overflow-tooltip="true" prop="migrationType" label="迁移类型" min-width="150" />
      <el-table-column v-if="columns.visible('status')" label="测试状态" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 'TODO'" type="info">待测试</el-tag>
          <el-tag v-if="scope.row.status === 'PROCESSING'">测试中</el-tag>
          <el-tag v-if="scope.row.status === 'FINISH'" type="success">完成</el-tag>
          <el-tag v-if="scope.row.status === 'FAIL'" type="danger">失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('testResult')" prop="testResult" label="测试进度" min-width="280">
        <template slot-scope="scope">
          <el-tag type="info">总数：{{ scope.row.testTotal }}</el-tag>
          <el-tag type="success" style="margin: 0px 5px">测试成功: {{ scope.row.testSuccessCount }}</el-tag>
          <el-tag type="danger">测试失败: {{ scope.row.testFailCount }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('startTime')" prop="startTime" label="开始时间" min-width="150">
        <template slot-scope="scope">
          <div>
            <i class="el-icon-time" />
            <span style="margin-left: 5px">{{ scope.row.startTime }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('endTime')" prop="endTime" label="结束时间" min-width="150">
        <template slot-scope="scope">
          <div>
            <i class="el-icon-time" />
            <span style="margin-left: 5px">{{ scope.row.endTime }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('upUserName')" prop="upUserName" label="更新人" />
      <el-table-column v-if="columns.visible('upTs')" prop="upTs" label="更新时间" min-width="150" />
      <el-table-column label="详情">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="toShowDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination />
    <!-- 新增页面 -->
    <el-dialog
      v-if="crud.status.cu === 1"
      v-loading="loading"
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="1200px"
      top="5vh"
      @loadingChange="changeLoading"
    >
      <el-form ref="form" :model="form" :rules="rules" :inline="true" label-width="150px">
        <el-row>
          <el-col :span="22">
            <el-steps :active="active" finish-status="success" simple>
              <el-step title="基本信息" />
              <el-step title="数据对齐" />
            </el-steps>
          </el-col>
          <el-col :span="2">
            <div class="grid-content bg-purple-light">
              <el-button
                icon="el-icon-question"
                class="assist"
                @click="helpFormVisible = true"
              >帮助
              </el-button>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <div v-if="active===0">
            <basic-info ref="basicInfo" v-model="form" :crud="crud" />
            <div class="dialog-footer" style="text-align: center;margin-top: 10px;">
              <el-button @click="crud.cancelCU">关 闭</el-button>
              <el-button type="primary" @click="next">下一步</el-button>
            </div>
          </div>
          <div v-if="active===1">
            <data-alignment v-model="form" />
            <div class="dialog-footer" style="text-align: center;margin-top: 40px;">
              <el-button @click="prev">上一步</el-button>
              <el-button type="primary" @click="submitCU()">保 存</el-button>
            </div>
          </div>
        </el-row>
      </el-form>
    </el-dialog>
    <!--  详情  -->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="detailClose"
      :visible.sync="showDetailFlag"
      title="脚本测试结果详情"
      top="5vh"
      width="1100px"
    >
      <el-tabs>
        <el-tab-pane label="脚本测试" name="first" />
      </el-tabs>
      <test-result v-if="showDetailFlag" :test-script-task-pk-id="showTestScriptTaskPkId" />
    </el-dialog>
    <!-- 查看帮助文档 -->
    <el-dialog title="帮助" :visible.sync="helpFormVisible" width="1200px">
      <div>
        <script-test-helper />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/expand/script/test/CRUD.operation'
import pagination from '@crud/Pagination'
import scriptDataTestTaskApi from '@/api/script/test/scriptDataTestTask'
import validateTaskName from '@/utils/taskNameRepeat'
import basicInfo from './components/basicInfo/index'
import dataAlignment from './components/dataAlignment/index'
import scriptDataTestListApi from '@/api/script/test/scriptDataTestList'
import testResult from './components/testResult'
import scriptTestHelper from '@views/script/test/components/helper/helper.vue'

const defaultCrud = CRUD({ title: '脚本测试任务', url: '/api/script/test/task', crudMethod: { ...scriptDataTestTaskApi }})
const defaultForm = {
  pkId: null,
  taskName: null,
  scriptSelectType: '直连',
  sourceConnPkId: null,
  database: null,
  isDataAlignment: 'Y',
  dataSyncType: 'TODAY',
  status: 'TODO',
  testTotal: 0,
  testSuccessCount: 0,
  testFailCount: 0,
  customTime: null,
  testScriptList: []
}
export default {
  components: { scriptTestHelper, crudOperation, rrOperation, pagination, basicInfo, dataAlignment, testResult },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      active: 0,
      rules: {
        taskName: [
          { required: true, validator: validateTaskName.bind(this, scriptDataTestTaskApi, this.form), trigger: 'blur' }
        ],
        scriptSelectType: [
          { required: true, message: '请选择脚本选择方式', trigger: 'change' }
        ],
        isDataAlignment: [
          { required: true, message: '请选择是否数据对齐', trigger: 'change' }
        ],
        dataSyncType: [
          { required: true, message: '请选择日期', trigger: 'blur' }
        ]
      },
      permission: {
        add: ['admin', 'scriptTest:add'],
        edit: ['admin', 'scriptTest:edit'],
        del: ['admin', 'scriptTest:del']
      },
      statusOptions: [
        { key: 'TODO', label: '待测试' },
        { key: 'PROCESSING', label: '测试中' },
        { key: 'FINISH', label: '完成' },
        { key: 'FAIL', label: '失败' }
      ],
      showDetailFlag: false,
      showTestScriptTaskPkId: null,
      dataTransferAnalysisFlag: false,
      helpFormVisible: false,
      loading: false
    }
  },
  created() {
    this.crud.resetQuery(false)
  },
  methods: {
    [CRUD.HOOK.beforeToEdit](crud) {
      if (crud.form.pkId !== null) {
        // 用于编辑功能时获取任务名称
        crud.form.validateName = this.form.taskName
        const params = { taskPkId: crud.form.pkId }
        scriptDataTestListApi.queryList(params).then(data => {
          crud.form.testScriptList = data
        })
      }
    },
    [CRUD.HOOK.beforeToCU](crud) {
      this.active = 0
    },
    next() {
      this.$refs.basicInfo.resetQuery()
      this.$refs['form'].validate((valid) => {
        if (valid && this.checkSelectScript()) {
          if (this.crud.form.pkId === null) {
            this.toAdd()
          } else {
            this.toEdit()
          }
        }
      })
    },
    toAdd() {
      scriptDataTestTaskApi.add(this.crud.form).then(res => {
        this.crud.form.pkId = res
        this.active = 1
      })
    },
    toEdit() {
      scriptDataTestTaskApi.edit(this.crud.form).then(res => {
        this.active = 1
      })
    },
    submitCU() {
      scriptDataTestTaskApi.edit(this.crud.form).then(res => {
        this.crud.editSuccessNotify()
        this.crud.cancelCU()
        this.crud.toQuery()
      })
    },
    checkSelectScript() {
      if (this.form.testScriptList.length < 1) {
        this.$notify({ title: '告警', message: '请选择要测试的脚本！', type: 'warning' })
        return false
      }
      return true
    },
    prev() {
      this.crud.form.validateName = this.form.taskName
      this.active = 0
    },
    detailClose() {
      this.showDetailFlag = false
    },
    toShowDetail(data) {
      this.showTestScriptTaskPkId = data.pkId
      this.showDetailFlag = true
    },
    changeLoading(status) {
      this.loading = status
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-transfer-panel {
  width: 250px;
}

::v-deep .bordered-button {
  border: #92dab3 2px;
}

::v-deep .el-transfer__buttons button {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

::v-deep .el-transfer__buttons button:first-child span span {
  margin-left: 0px;
}

.assist {
  min-height: 46px;
  color: #fff;
  background-color: #ff4949;
  border-color: #ff4949;
}
</style>
